package com.it;

import java.util.Arrays;
import java.util.Random;

public class MaxOfArray {
    public static void main(String[] args) {
        //静态数组
        int[] arr1 = {1, 2, 3, 5, 6, 8, 7, 9, 4, 3, 2};
        int[] arr3 = {1, 2, 3, 5, 6, 8, 7, 9, 4, 3, 2};
        //排序,从小到大排序
        Arrays.sort(arr1);
        System.out.println(arr1);//这里打印的是地址值
        System.out.println(arr3);//这里打印的是地址值

        String string = Arrays.toString(arr1);//打印数值
        System.out.println("string = " + string);


        //动态数组
        int[] arr2 = new int[5];
        for (int i = 0; i < arr2.length; i++) {
            int random = (int) (Math.random() * 100);// 确认随机数范围0-99
            arr2[i] = random;//赋值
            System.out.println("第" + i + "索引值为" + arr2[i]);
        }

        //找数组最大值
        int max = arr2[0];//假设数组的第0个索引就是最大值
        int index = 0;//假设数组的最大值就是第0个索引

        //这里已经假设第0个索引为最大值了，可以从第一个索引比较
        for (int i = 1; i < arr2.length; i++) {
            if (arr2[i] > max) {
                max = arr2[i];//把新的最大值赋值给max
                index = i;//同时把新的索引赋值给index
            }
        }
        System.out.println("max = " + max + "\tindex = " + index);


        //数组的复制,更加灵活
        int[] a = {10, 20, 30, 11, 22, 33, 55, 0, 4};
        int[] b = new int[10];

        //a,源数组
        //1,源数组的起始索引
        //b.目标数组
        //0,目标数组的起始索引
        //5,要复制的元素个数

        //注意：防止索引越界问题
        System.arraycopy(a, 1, b, 1, 5);
        System.out.println(Arrays.toString(b));//[0, 20, 30, 11, 22, 33, 0, 0, 0, 0]


        //数组复制
        int[] c = {10, 20, 30, 40, 50};
        int[] d = Arrays.copyOf(c, 6);
        int[] e = Arrays.copyOf(c, 4);
        //c,源数组
        //d,目标数组
        //6,目标数组的长度
        //若目标数组的长度>源数组长度，则末尾补默认值
        //若目标数组的长度<源数组长度，则将末尾的截掉
        System.out.println(Arrays.toString(d));//[10, 20, 30, 40, 50, 0]
        System.out.println(Arrays.toString(e));//[10, 20, 30, 40]


        //随机数加入数组
        Random random = new Random();
        int[] array1s = new int[10];
        for (int i1 = 0; i1 < array1s.length; i1++) {
            int i = random.nextInt(100);//确定取值范围在0-99之间
            array1s[i1] = i;//把随机数赋值到元素上
            System.out.println(array1s[i1]);
        }
    }
}
